extends Area2D

@export var Card_Element_Type = 0
@export var Card_Facing_Up = 0
@export var Card_Element_Name = ["Fire","Water","Gold"]
@export var Move_Speed = 0.25
var Card_Id = 0
var Card_Is_Selected = 0
var mouse_over = 0
var Card_In_Hand = 0
var Final_Position = Vector2(0,0)
var eps = 1e-3
@onready var Card_Background = $Card_Background
@onready var Card_Element = $Card_Element
@onready var Root_Node = self


# Called when the node enters the scene tree for the first time.

func Distance(a:Vector2,b:Vector2) -> float:
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))

func Check_Position() -> bool:
	if Distance(self.position,Final_Position) > eps :
		return false
	else :
		return true

func Move() -> void:
	if Check_Position() :
		self.position = Final_Position
	else :
		self.position = lerp(self.position,Final_Position,Move_Speed)
	
func _ready() -> void:
	Card_Element_Type = randi()%Card_Element_Name.size()

func _on_mouse_entered() -> void:
	mouse_over = true

func _on_mouse_exited() -> void:
	mouse_over = false

func _on_input_event(viewport, event, shape_idx) -> void:
	if event is InputEventMouseButton and mouse_over :
		if event.pressed and event.button_index == MOUSE_BUTTON_LEFT and Card_In_Hand:
			Card_Is_Selected = 1 - Card_Is_Selected
			if Card_Is_Selected:
				Final_Position.y -= 25
			else:
				Final_Position.y += 25

func _process(delta: float) -> void:
	if Card_Facing_Up:
		Card_Background.animation = "Card_front"
		# Card_Background.visible = 1
		Card_Element.visible = 1
	else:
		Card_Background.animation = "Card_back"
		# Card_Background.visible = 0
		Card_Element.visible = 0
	
	Card_Element.animation = Card_Element_Name[Card_Element_Type]
	
	Move()
